//
//  EPMNetWorkTool.m
//  WeiBoByOC
//
//  Created by 马继鵬 on 17/3/23.
//  Copyright © 2017年 7TH. All rights reserved.
//

#import "EPMNetWorkTool.h"

@protocol HTTPProxy <NSObject>
@optional
- (NSURLSessionDataTask*)dataTaskWithHTTPMethod:(NSString *)method
                                      URLString:(NSString *)URLString
                                     parameters:(id)parameters
                                 uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
                               downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
                                        success:(void (^)(NSURLSessionDataTask *, id))success
                                        failure:(void (^)(NSURLSessionDataTask *, NSError *))failure;

@end
@interface EPMNetWorkTool()<HTTPProxy>

@end

@implementation EPMNetWorkTool

    + (instancetype)sheardTool
    {
        static EPMNetWorkTool* tool;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            tool = [EPMNetWorkTool new];
            tool.responseSerializer.acceptableContentTypes = [tool.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
        });
        return tool;
        
    }
    
    
    - (void)request:(Method)method url:(NSString *)urlString paramater:(id)paramater finisedBlock:(void (^)(id, NSError *))finiesedBlock
    {
        NSString* methodName =  ( method == GET ? @"GET" : @"POST");
        
        //成功回调
        void(^succesBlock)(NSURLSessionTask*, id) = ^ (NSURLSessionTask* task, id respond){
            
            finiesedBlock( respond , nil);
        };
        //失败回调
        
        void(^failBlock)(NSURLSessionTask* , NSError*) = ^ (NSURLSessionTask* task, NSError* error){
            finiesedBlock(nil,error);
        };
        
      [[self dataTaskWithHTTPMethod:methodName URLString:urlString parameters:paramater uploadProgress:nil downloadProgress:nil success:succesBlock failure:failBlock]resume];
    }

#pragma
#pragma-home数据下载
- (void)getHomeWeiBoDataMaxID:(UInt64)maxID sinceID:(UInt64)sinceID finished:(void (^)(id, NSError *))finishedBlock{
    NSString* urlString = @"https://api.weibo.com/2/statuses/home_timeline.json";
    NSDictionary* para = @{
                           @"access_token":[EPMUserAccountModelView sheardTool].accessToken,
                           @"since_id": @(sinceID),
                           @"max_id":@(maxID)
                           };
    [self request:GET url:urlString paramater:para finisedBlock:finishedBlock];
}
    
#pragma
#pragma-发送weinbo
//带图
- (void)sendMessageWithMessage:(NSString *)message pictures:(NSArray<UIImage *> *)imgList finished:(void (^)(id, NSError *))finishedBlock{
    NSString* URLString = @"https://upload.api.weibo.com/2/statuses/upload.json";
    NSDictionary* para = @{
                           @"access_token": [EPMUserAccountModelView sheardTool].accessToken,
                           @"status": message
                           };
    [self POST:URLString parameters:para constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        for(UIImage* img in imgList){
            NSData* data = UIImagePNGRepresentation(img);
            [formData appendPartWithFileData:data name:@"pic" fileName:@"suibian" mimeType:@"application/actet-stream"];
        }
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        finishedBlock(responseObject,nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        finishedBlock(nil,error);
    }];
}

//不带图
- (void)sendMessageWithMessage:(NSString *)message finished:(void (^)(id, NSError *))finishedBlock{
    NSString* URLString = @"https://api.weibo.com/2/statuses/update.json";
    NSDictionary* para = @{
                           @"access_token": [EPMUserAccountModelView sheardTool].accessToken,
                           @"status": message
                           };
    [self request:POST url:URLString paramater:para finisedBlock:finishedBlock];
}





@end
